/*---------------------------------------------------------------------------*/
/*!				\file				SystemHooks.c
				\date				march 2010
				\author				TNick

				\brief				Interaction with input devices at low level
    
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */
	#include	"Config.h"
	#include	<windows.h>
	#include	<Commctrl.h>
	
	#include	"main.h"
	
/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */

/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */

/*  DATA    ================================================================ */
//
//
//
//
/*  FUNCTIONS    ----------------------------------------------------------- */


/* ------------------------------------------------------------------------- */
LRESULT CALLBACK MouseProc			(int nCode, WPARAM wParam, LPARAM lParam) 
{
	
	if (wParam == WM_MOUSEMOVE)
	{
		GST_ROW CrtIdx = GST_ROW_INVALID;
		if (((MSLLHOOKSTRUCT*)lParam)->pt.x <= SQUARE_SIZE)
		{
			if (((MSLLHOOKSTRUCT*)lParam)->pt.y <= SQUARE_SIZE)
			{
				CrtIdx = GST_ROW_TL;
			}
			else if (((MSLLHOOKSTRUCT*)lParam)->pt.y >= cyScreen - SQUARE_SIZE)
			{
				CrtIdx = GST_ROW_BL;
			}
		}
		else if (((MSLLHOOKSTRUCT*)lParam)->pt.x >= cxScreen - SQUARE_SIZE)
		{
			if (((MSLLHOOKSTRUCT*)lParam)->pt.y <= SQUARE_SIZE)
			{
				CrtIdx = GST_ROW_TR;
			}
			else if (((MSLLHOOKSTRUCT*)lParam)->pt.y >= cyScreen - SQUARE_SIZE)
			{
				CrtIdx = GST_ROW_BR;
			}
		}
		if (CrtIdx == GST_ROW_INVALID) goto DoDefault;
		
		if (GestureStart == GST_ROW_INVALID)
		{
			if (CurrentState == STATE_HIDDEN)
			{
				if (AppStgs.ShowGest[CrtIdx] == 0) goto DoDefault;
			}
			else
			{
				if (AppStgs.HideGest[CrtIdx] == 0) goto DoDefault;
			}
			
			GestureStart = CrtIdx;
			if (gTimer != 0) KillTimer(hwMain,ID_TIMER_GESTURE);
			AppStgs.EpocsToWait = AppStgs.EpocsToWait;
			gTimer = SetTimer(hwMain,ID_TIMER_GESTURE,TIMER_RESOLUTION,NULL);
		}
		else if (GestureStart != CrtIdx)
		{
			
			if (CurrentState == STATE_HIDDEN)
			{
				if (AppStgs.Flags & APPFL_SHOWGEST)
				{
					if (AppStgs.ShowGest[CrtIdx] == 0) goto DoDefault;
					// ok; there we go; we have a gesture
					GestureStart = GST_ROW_INVALID;
					Epcs = 0;
					KillTimer(hwMain, ID_TIMER_GESTURE);
					gTimer = 0;
					Do_UnHideSelected();
				}
			}
			else
			{
				if (AppStgs.Flags & APPFL_HIDEGEST)
				{
					if (AppStgs.HideGest[CrtIdx] == 0) goto DoDefault;
					// ok; there we go; we have a gesture
					GestureStart = GST_ROW_INVALID;
					Epcs = 0;
					KillTimer(hwMain, ID_TIMER_GESTURE);
					gTimer = 0;
					Do_HideSelected();
				}
			}
		}
	}

	
	DoDefault:
	return CallNextHookEx(g_mouseHook, nCode, wParam, lParam); 
}
/* ========================================================================= */


/* ------------------------------------------------------------------------- */
LRESULT CALLBACK KeyboardCallback	(int nCode, WPARAM wParam, LPARAM lParam) 
{
	SHRC LocS;
	
	if (wParam != WM_KEYDOWN) goto DoDefault;
	
	LocS.Letter = (char)((KBDLLHOOKSTRUCT *)lParam)->vkCode;
	
	if (GetAsyncKeyState(VK_CONTROL) & 0x80000000) LocS.Ctrl = 1;
	else LocS.Ctrl = 0;
	if (GetAsyncKeyState(VK_MENU) & 0x80000000) LocS.Alt = 1;
	else LocS.Alt = 0;
	if (GetAsyncKeyState(VK_SHIFT) & 0x80000000) LocS.Shift = 1;
	else LocS.Shift = 0;
	
	if (CurrentState == STATE_UNHIDDEN)
	{
		if ((LocS.Letter == AppStgs.HideSrg.Letter) && 
			(LocS.Ctrl == AppStgs.HideSrg.Ctrl) && 
			(LocS.Alt == AppStgs.HideSrg.Alt) && 
			(LocS.Shift == AppStgs.HideSrg.Shift))
			Do_HideSelected();
	}
	else
	{
		if ((LocS.Letter == AppStgs.ShowSrg.Letter) && 
			(LocS.Ctrl == AppStgs.ShowSrg.Ctrl) && 
			(LocS.Alt == AppStgs.ShowSrg.Alt) && 
			(LocS.Shift == AppStgs.ShowSrg.Shift))
			Do_UnHideSelected();
	}
	DoDefault:
	return CallNextHookEx(g_keyboardHook, nCode, wParam, lParam); 
}
/* ========================================================================= */

/*  FUNCTIONS    =========================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
